//
//  WMEFirstPageViewController.m
//  WMEMusic
//
//  Created by 恩叔 on 16/4/12.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEFirstPageViewController.h"
#import "WMEFirstPageCollectionViewCell.h"
#import "WMEFirstTwoPageCollectionViewCell.h"
#import "WMESongListViewController.h"
#import "WMEFirstPageModel.h"
#import "WMEMyBaseViewController.h"
#import "WMEHotListViewController.h"
#import "SearchViewController.h"
@interface WMEFirstPageViewController ()<UICollectionViewDataSource ,UICollectionViewDelegate>
@property (nonatomic, strong)UIButton *songMenuBtn;/**<歌单*/
@property (nonatomic, strong)UIButton *listBtn;/**<榜单*/
@property (nonatomic, strong)UIView *line;
@property (nonatomic, strong)UICollectionView *collectionView;

@end

@implementation WMEFirstPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"吴明恩的音乐盒子";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的" style:UIBarButtonItemStyleDone target:self action:@selector(xiazai:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStyleDone target:self action:@selector(searchAction:)];
    [self createView];
}
- (void)createView
{
    WS(ws);
    self.songMenuBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_songMenuBtn];
    [_songMenuBtn setTitle:@"歌单" forState:UIControlStateNormal];
    [_songMenuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_songMenuBtn addTarget:self action:@selector(songMenuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_songMenuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@60);
        make.height.equalTo(@30);
        make.left.equalTo(ws.view.mas_left).with.offset(WIDTH / 2 - 100);
    }];
    
    self.listBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_listBtn];
    [_listBtn setTitle:@"榜单" forState:UIControlStateNormal];
    [_listBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_listBtn addTarget:self action:@selector(listBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_listBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@60);
        make.height.equalTo(@30);
        make.right.equalTo(ws.view.mas_right).with.offset(- (WIDTH / 2 - 100));
    }];
    
    self.line = [[UIView alloc] init];
    _line.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_line];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(WIDTH / 2 - 100);
        make.width.equalTo(@60);
        make.height.equalTo(@1);
        make.top.equalTo(ws.view.mas_top).with.offset(29);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH, HEIGHT - 94);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(30);
    }];
    _collectionView.contentSize = CGSizeMake(WIDTH *2, HEIGHT - 94);
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[WMEFirstPageCollectionViewCell class] forCellWithReuseIdentifier:@"firstOneCell"];
    [_collectionView registerClass:[WMEFirstTwoPageCollectionViewCell class] forCellWithReuseIdentifier:@"firstTwoCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        WMEFirstPageCollectionViewCell *firstOneCell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"firstOneCell" forIndexPath:indexPath];
        void(^block)() = ^(WMEFirstPageModel *model){
            WMESongListViewController *songList = [[WMESongListViewController alloc] init];
            songList.model = model;
            [self.navigationController pushViewController:songList animated:YES];
        };
        firstOneCell.firstBlock = block;
        return firstOneCell;
    }
    if (indexPath.row == 1) {
        WMEFirstTwoPageCollectionViewCell *firstTwoCell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"firstTwoCell" forIndexPath:indexPath];
        void(^block2)() = ^(WMEFirstPageModel *model){
            WMEHotListViewController *hotList = [[WMEHotListViewController alloc] init];
            hotList.model = model;
            [self.navigationController pushViewController:hotList animated:YES];
        };
        firstTwoCell.twoBlock = block2;
        return firstTwoCell;
    }
    return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    WS(ws);
    CGFloat pointX = _collectionView.contentOffset.x;
    if (pointX > WIDTH / 2) {
        [_listBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_songMenuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }else {
        [_songMenuBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_listBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    }
    [_line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(( WIDTH / 2 - 100) + 140*( pointX / WIDTH));
    }];
}

- (void)songMenuBtnAction:(UIButton *)btn
{
    [_songMenuBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_listBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.5f animations:^{
        _line.frame = CGRectMake(WIDTH / 2 - 100, 29, 60, 1);
    }];
    _collectionView.contentOffset = CGPointMake(0, 0);
}

- (void)listBtnAction:(UIButton *)btn
{
    [_listBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_songMenuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.5f animations:^{
        _line.frame = CGRectMake(WIDTH / 2  , 29, 60, 1);
    } completion:^(BOOL finished) {
        
    }];
    _collectionView.contentOffset = CGPointMake(WIDTH, 0);
}
- (void)xiazai:(UIBarButtonItem *)btn
{
    WMEMyBaseViewController *xiazai = [[WMEMyBaseViewController alloc] init];
    [self.navigationController pushViewController:xiazai animated:YES];
}
- (void)searchAction:(UIBarButtonItem *)btn
{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
